home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Eudora 1.3.1 / source / nickexp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-16  |  6.9 KB  |  266 lines  |  [TEXT/MPS ]

  1. #define FILE_NUM 6
  2. /* Copyright (c) 1990-1992 by the University of Illinois Board of Trustees */
  3. /**********************************************************************
  4.  * expansion of nicknames
  5.  **********************************************************************/
  6. #pragma load EUDORA_LOAD
  7. #pragma segment NickExp
  8.  
  9. Boolean AliasRecursion;
  10. /************************************************************************
  11.  * ExpandAliases - take an address list (as from SuckAddresses), and
  12.  * expand it using the alias list
  13.  ************************************************************************/
  14. UHandle  ExpandAliases(UHandle    fromH,short depth,Boolean wantComments)
  15. {
  16.     int err=0;
  17.     UHandle toH,spewHandle;
  18.     long offset, newOffset;
  19.     
  20.     if (++depth > MAX_DEPTH) return(nil);
  21.     AliasRecursion = 0;
  22.     if (!(err=RegenerateAliases(&Aliases,ALIAS_CMD)))
  23.     {
  24.         AliasRefCount++;
  25.         toH = NuHandle(0L);
  26.         if (!toH)
  27.             WarnUser(ALLO_EXPAND,err=MemError());
  28.         else
  29.         {
  30.             for (offset=0;(*fromH)[offset]; offset += (*fromH)[offset]+2)
  31.             {
  32.                 /*
  33.                  * get rid of the extraneous stuff
  34.                  */
  35.                 spewHandle = SuckPtrAddresses(LDRef(fromH)+offset+1,(*fromH)[offset],False);
  36.                 UL(fromH);
  37.                 if (!spewHandle) {err=1;break;}
  38.                 newOffset=FindExpansionFor(Aliases,LDRef(spewHandle)+1,**spewHandle);
  39.                 DisposHandle(spewHandle);
  40.                 if (newOffset)
  41.                 {
  42.                     UHandle lookup,result;
  43.                     lookup = SuckPtrAddresses(LDRef(Aliases)+newOffset+2,
  44.                                          (*Aliases)[newOffset]*256+(*Aliases)[newOffset+1],wantComments);
  45.                     UL(Aliases);
  46.                     if (lookup==nil) err=1;
  47.                     else
  48.                     {
  49.                         /*
  50.                         * now, expand the addresses in the expansion
  51.                         */
  52.                         result=ExpandAliases(lookup,depth,wantComments);
  53.                         if (!result)
  54.                             if (!(err = MemError()))
  55.                             {
  56.                                 err = 1;
  57.                                 if (!AliasRecursion)
  58.                                 {
  59.                                     Str255 msg;
  60.                                     Str255 alias;
  61.                                     GetRString(msg,ALIA_LOOP);
  62.                                     BlockMove(&(*fromH)[offset],alias,(*fromH)[offset]+1);
  63.                                     MyParamText(msg,alias,"","");
  64.                                     (void) ReallyDoAnAlert(OK_ALRT,Stop);
  65.                                     AliasRecursion = 1;
  66.                                 }
  67.                             }
  68.                         DisposHandle(lookup);
  69.                         
  70.                         /*
  71.                          * add the expanded aliases to the new list
  72.                          */
  73.                         if (!err && result)
  74.                         {
  75.                             SetHandleBig(result,GetHandleSize(result)-1);
  76.                             HLock(result);
  77.                             HandAndHand(result,toH);
  78.                             err = MemError();
  79.                             DisposHandle(result);
  80.                         }
  81.                         else if (!err)
  82.                             err = MemError();
  83.                     }
  84.                 }
  85.                 else
  86.                 {
  87.                     Str63 temp;
  88.                     /*
  89.                      * original was NOT an alias; just copy it
  90.                      */
  91.                     /* if it is "me" then the user has not defined me as a nickname.
  92.                          just put in the return address. */
  93.                     GetRString(temp, ME);
  94.                     LDRef(fromH);
  95.                     if (EqualString(temp,(*fromH)+offset,True,True))
  96.                     {
  97.                         GetReturnAddr(temp, True);
  98.                         err = PtrAndHand(temp, toH, temp[0]+2);
  99.                     }
  100.                     else
  101.                         err=PtrAndHand((*fromH)+offset,toH,(*fromH)[offset]+2);
  102.                     HUnlock(fromH);
  103.                 }
  104.                 if (err && err!=1) {WarnUser(ALLO_EXPAND,err); break;}
  105.             }
  106.         }
  107.         if (Aliases && AliasRefCount && !--AliasRefCount) HPurge(Aliases);
  108.     }
  109.     if (!err)
  110.     {
  111.         SetHandleBig(toH,GetHandleSize(toH)+1);
  112.         if (!(err=MemError())) (*toH)[GetHandleSize(toH)-1] = 0;
  113.     }
  114.     if (err)
  115.     {
  116.         DisposHandle(toH);
  117.         AliasRecursion = 1;
  118.     }
  119.     return(err ? nil : toH);
  120. }
  121.  
  122. /************************************************************************
  123.  * FindAliasFor - find an alias.    Returns the offset into the Aliases block.
  124.  *    returns -1 if none found.
  125.  ************************************************************************/
  126. long FindAliasFor(UHandle aliases,UPtr name,short size)
  127. {
  128.     long max = GetHandleSize(aliases);
  129.     long offset;
  130.     Str255 pName;
  131.     
  132.     BlockMove(name,pName+1,size);
  133.     *pName = size;
  134.     /*RemoveParens(pName);*/
  135.     
  136.     for (offset=0;
  137.                 offset<max;
  138.                 offset+=CountAliasTotal(aliases,offset))
  139.         if (EqualString(pName,(*aliases)+offset,False,False)) break;
  140.     if (offset<max) return(offset);
  141.     else return(-1);
  142. }
  143.  
  144. /************************************************************************
  145.  * FindExpansionFor - find the expansion for an alias.    Returns an
  146.  * offset into the Aliases block.  returns 0 if none found.
  147.  ************************************************************************/
  148. long FindExpansionFor(UHandle aliases,UPtr name,short size)
  149. {
  150.     long offset = FindAliasFor(aliases,name,size);
  151.  
  152.     if (offset>=0) return(offset+CountAliasAlias(aliases,offset)+1);
  153.     else return(0);
  154. }
  155. /************************************************************************
  156.  * FinishAlias - finish a partially completed alias
  157.  ************************************************************************/
  158. void FinishAlias(MyWindowPtr win, Boolean wantExpansion, Boolean findOnly)
  159. {
  160.     Str63 word;
  161.     UPtr spot, begin, end;
  162.     TEHandle teh=WinTEH(win);
  163.     long offset,max;
  164.     short found=0;
  165.     short which,current;
  166.     long foundOffset=0;
  167.     uShort save;
  168.     
  169.     if (!teh) return;
  170.     
  171.     begin = *(*teh)->hText;
  172.     if ((*teh)->selStart != (*teh)->selEnd)
  173.     {
  174.         spot = begin+(*teh)->selStart;
  175.         end = begin+(*teh)->selEnd-1;
  176.     }
  177.     else
  178.     {
  179.         spot = end = *(*teh)->hText + (*teh)->selStart - 1;
  180.         while(spot>=begin && *spot>' ' && *spot!=',' && *spot!='(') spot--;
  181.         spot++;
  182.         if (spot>end) return;
  183.     }
  184.     *word = MIN(sizeof(word)-1,end-spot+1);
  185.     BlockMove(spot,word+1,*word);
  186.     
  187.     if (RegenerateAliases(&Aliases,ALIAS_CMD)) return;
  188.     
  189.     max = GetHandleSize(Aliases);
  190.     for (offset=0,current=0;
  191.                 offset<max;
  192.                 offset+=CountAliasTotal(Aliases,offset),current++)
  193.     {
  194.         save = (*Aliases)[offset];
  195.         if (save<*word) continue;
  196.         (*Aliases)[offset] = *word;
  197.         if (EqualString(word,(*Aliases)+offset,False,True))
  198.         {
  199.             if (!found)
  200.             {
  201.                 foundOffset=offset;
  202.                 which=current;
  203.             }
  204.             else if (found == 1 && !findOnly)
  205.                 SysBeep(20);
  206.             found++;
  207.         }
  208.         (*Aliases)[offset] = save;
  209.     }
  210.     
  211.     if (found == 1 && !findOnly)
  212.     {
  213.         TESetSelect(spot-begin,end-begin+1,teh);
  214.         InsertAlias(win,foundOffset,wantExpansion);
  215.     }
  216.     if (found && AliasWinIsOpen() && win->qWindow.windowKind!=ALIAS_WIN)
  217.         ChooseAlias(which+1);
  218.     HPurge(Aliases);
  219. }
  220.  
  221. /************************************************************************
  222.  * InsertAlias - insert an alias in a window
  223.  ************************************************************************/
  224. void InsertAlias(MyWindowPtr win, long foundOffset,Boolean wantExpansion)
  225. {
  226.     short i,n;
  227.     UHandle wordH,list;
  228.     UPtr spot,begin;
  229.     
  230.     Undo.didClick = True;
  231.     if (!wantExpansion)
  232.     {
  233.         n = CountAliasAlias(Aliases,foundOffset);
  234.         for (i=1;i<=n;i++)
  235.             TESomething(win,TEKEY,(*Aliases)[foundOffset+i],0);
  236.     }
  237.     else
  238.     {
  239.         if (!(wordH=NuHandle((*Aliases)[foundOffset]+3)))
  240.             WarnUser(MEM_ERR,MemError());
  241.         else
  242.         {
  243.             PCopy(*wordH,(*Aliases)+foundOffset);
  244.             (*wordH)[**wordH+1] = (*wordH)[**wordH+2] = 0;
  245.             list = ExpandAliases(wordH,0,True);
  246.             DisposHandle(wordH);
  247.             if (list)
  248.             {
  249.                 Undo.didClick = True;
  250.                 begin=LDRef(list);
  251.                 for (spot=begin; *spot; spot++)
  252.                 {
  253.                     if (spot!=begin)
  254.                     {
  255.                         TESomething(win,TEKEY,',',0);
  256.                         TESomething(win,TEKEY,' ',0);
  257.                     }
  258.                     spot++;
  259.                     while (*spot) TESomething(win,TEKEY,*spot++,0);
  260.                 }
  261.                 DisposHandle(list);
  262.             }
  263.         }
  264.     }
  265. }
  266.